package com.sunshine.swagger.controller;

import com.sunshine.swagger.bean.JsonResult;
import com.sunshine.swagger.bean.User;
import com.sunshine.swagger.enums.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WeiHong
 * @date 2019/8/14 15:37
 */
@RestController
@Log4j2
@Api(value = "UserController", tags = "用户-用户CURD")
@RequestMapping("user")
public class UserController {

    /**
     * 创建线程安全的Map
     */
    static Map<Integer, User> users = Collections.synchronizedMap(new HashMap<>());


    /**
     * 查询用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取用户详细信息", notes = "根据请求的url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("user/{id}")
    public ResponseEntity<JsonResult> getUserById(@PathVariable(value = "id") Integer id) {
        JsonResult r = new JsonResult();
        try {
            User user = users.get(id);
            r.setResult(user);
            r.setStatus(ResultEnum.OK.getCode());
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(ResultEnum.ERROR.getCode());
            log.error("查询用户信息错误，输入ID={}", id, e);
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping(value = "users")
    public ResponseEntity<JsonResult> getUserList() {
        JsonResult r = new JsonResult();
        try {

            ArrayList<User> users = new ArrayList<>(UserController.users.values());
            r.setResult(users);
            r.setStatus(ResultEnum.OK.getCode());
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(ResultEnum.ERROR.getCode());
            log.error("获取用户列表错误", e);
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping(value = "user")
    public ResponseEntity<JsonResult> add(@RequestBody User user) {
        JsonResult r = new JsonResult();
        try {
            users.put(user.getId(), user);
            r.setResult(user.getId());
            r.setStatus(ResultEnum.OK.getCode());
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(ResultEnum.ERROR.getCode());
            log.error("添加用户错误，输入参数: user={}", user.toString(), e);
        }
        return ResponseEntity.ok(r);
    }


    /**
     * 根据id修改用户信息
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "更新信息", notes = "根据url的id来指定更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User")
    })
    @RequestMapping(value = "user/{id}", method = RequestMethod.PUT)
    public ResponseEntity<JsonResult> update(@PathVariable("id") Integer id, @RequestBody User user) {
        JsonResult r = new JsonResult();
        try {
            User u = users.get(id);
            u.setUsername(user.getUsername());
            u.setAge(user.getAge());
            users.put(id, u);
            r.setResult(u);
            r.setStatus(ResultEnum.OK.getCode());
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(ResultEnum.ERROR.getCode());
            log.error("修改用户信息错误，输入id={},user={}", id, user.toString(), e);
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 使用该注解忽略这个API
     *
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String jsonTest() {
        return " hi you!";
    }

}
